package com.yonyougov.devops.cruxcisupport.thread;

import com.yonyougov.devops.cruxcisupport.commitcontext.CommitContextService;
import com.yonyougov.devops.cruxcisupport.commitcontext.vo.CommitContextInfoVo;
import com.yonyougov.devops.cruxcisupport.committest.CommitTestDao;
import com.yonyougov.devops.cruxcisupport.committest.CommitTestEntity;
import com.yonyougov.devops.cruxcisupport.enums.TestStatusEnum;
import com.yonyougov.devops.cruxcisupport.subscribe.SubscribeDao;
import com.yonyougov.devops.cruxcisupport.subscribe.SubscribeEntity;
import com.yonyougov.devops.cruxcisupport.user.UserDao;
import com.yonyougov.devops.cruxcisupport.user.UserEntity;
import com.yonyougov.devops.cruxcisupport.util.MailUtils;
import com.yonyougov.devops.cruxcisupport.util.excell.ExcellUtil;
import lombok.SneakyThrows;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 发送提测邮件
 */
public class SendCreateOrConfirmMailThread extends Thread {
    private MailUtils mailUtils;
    private String remark;
    private BigInteger commitTestId;
    private SubscribeDao subscribeDao;
    private CommitTestDao commitTestDao;
    private String from;
    private UserDao userDao;
    private String currrentUserName;
    private List<String> attachmentUrls;
    private int status;
    private List<String> excellPaths;
    private CommitContextService commitContextService;
    private ExcellUtil excellUtil;

    public SendCreateOrConfirmMailThread(MailUtils mailUtils, BigInteger commitTestId, SubscribeDao subscribeDao,
                                         CommitTestDao commitTestDao, CommitContextService commitContextService,
                                         String from, String remark, UserDao userDao,
                                         String currrentUserName, List<String> attachmentUrls, int status,
                                         List<String> excellPaths,ExcellUtil excellUtil) {
        this.mailUtils = mailUtils;
        this.commitTestId = commitTestId;
        this.remark = remark;
        this.subscribeDao = subscribeDao;
        this.commitTestDao = commitTestDao;
        this.from = from;
        this.userDao = userDao;
        this.currrentUserName = currrentUserName;
        this.attachmentUrls = attachmentUrls;
        this.status = status;
        this.excellPaths = excellPaths;
        this.commitContextService = commitContextService;
        this.excellUtil = excellUtil;
    }

    @SneakyThrows
    @Override
    public void run() {

        //根据提测Id查询提测记录
        CommitTestEntity commitTestEntity = new CommitTestEntity();
        commitTestEntity.setId(this.commitTestId);
        CommitTestEntity commitTest = commitTestDao.findBy(commitTestEntity);

        List<CommitContextInfoVo> contextInfoVos = commitContextService.
                findCommitContextByCommitTestId(commitTestEntity.getId());
        StringBuffer appName = new StringBuffer();
        StringBuffer versionName = new StringBuffer();
        StringBuffer branchName = new StringBuffer();
        Set<BigInteger> appIds = new HashSet<>();
        contextInfoVos.forEach(c -> {
            appName.append(c.getAppName()).append(";");
            versionName.append(c.getVersionName()).append(";");
            branchName.append(c.getTestBranch()).append(";");
            appIds.add(c.getAppId());
        });

        //查询订阅人
        List<SubscribeEntity> subscribeEntityList = subscribeDao.findByAppIds(appIds);
        //查询提测人
        UserEntity submitUser = new UserEntity();
        submitUser.setId(commitTest.getSubmitUserId());
        submitUser = userDao.findBy(submitUser);
        //获取收件人的邮箱地址
        Set<BigInteger> userIds = new HashSet<>();
        if (status == TestStatusEnum.COMMIT_START.getValue()) {
            // 开始提测
            userIds.add(commitTest.getSubmitUserId());
            userIds.add(commitTest.getTestLead());
            if (null != subscribeEntityList && subscribeEntityList.size() > 0) {
                subscribeEntityList.forEach(subscribe -> userIds.add(subscribe.getUserId()));
            }
        } else if (status == TestStatusEnum.FUNCTION_CONFIRM.getValue()) {
            // 功能待确认
            userIds.add(commitTest.getProductManager());
        }
        //根据用户Id查询用户信息
        List<UserEntity> userEntityList = userDao.findByIds(userIds);
        //获取邮箱列表
        String[] tos = userEntityList.stream().map(UserEntity::getEmail).toArray(String[]::new);
        // excell附件汇总
        if(excellPaths != null && excellPaths.size()>0){
            attachmentUrls.addAll(excellPaths);
        }
        excellPaths.add(excellUtil.cruxCommitTest(this.commitTestId));
        // 所有附件汇总
        if(null == attachmentUrls)attachmentUrls = new ArrayList<>();
        attachmentUrls.addAll(excellPaths);
        //发送邮件
        String subject = "";
        String content = "";
        if (status == TestStatusEnum.COMMIT_START.getValue()) {
            subject = "【提测】提测： " + appName + " " + versionName;
            content = "提测人： " + currrentUserName + " : " + appName + " 的 " + versionName + " 提测!详情如下: " + this.remark;
        } else if (status == TestStatusEnum.FUNCTION_CONFIRM.getValue()) {
            subject = "【提测】提测功能确认 " + appName + " " + versionName;
            content = "提测人： " + currrentUserName + " : " + appName + " 的 " + versionName + " 提测!详情如下: " + this.remark;
            content += "\n点击以下链接即代表功能完成确认 https://devopstest.yonyougov.top/crux-cisupport/api/v2/committest/ct/confirm?id="
                    + commitTest.getId();
            StringBuilder attachmentUrlsStr = new StringBuilder("&attachmentUrls=");
            for (String s : attachmentUrls) {
                attachmentUrlsStr.append(s).append(",");
            }
            content += attachmentUrlsStr;
        }
        mailUtils.sendEmailWithAttachments(this.from, tos, subject, content, attachmentUrls);
    }
}
